﻿#region Using directives

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Pharmacy.Entities;
using Pharmacy.Data;

#endregion

namespace Pharmacy.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="DeliveryNoteDetailsProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract class DeliveryNoteDetailsProviderBaseCore : EntityViewProviderBase<DeliveryNoteDetails>
	{
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions
		
		/*
		///<summary>
		/// Fill an VList&lt;DeliveryNoteDetails&gt; From a DataSet
		///</summary>
		/// <param name="dataSet">the DataSet</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList&lt;DeliveryNoteDetails&gt;"/></returns>
		protected static VList&lt;DeliveryNoteDetails&gt; Fill(DataSet dataSet, VList<DeliveryNoteDetails> rows, int start, int pagelen)
		{
			if (dataSet.Tables.Count == 1)
			{
				return Fill(dataSet.Tables[0], rows, start, pagelen);
			}
			else
			{
				return new VList<DeliveryNoteDetails>();
			}	
		}
		
		
		///<summary>
		/// Fill an VList&lt;DeliveryNoteDetails&gt; From a DataTable
		///</summary>
		/// <param name="dataTable">the DataTable that hold the data.</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList<DeliveryNoteDetails>"/></returns>
		protected static VList&lt;DeliveryNoteDetails&gt; Fill(DataTable dataTable, VList<DeliveryNoteDetails> rows, int start, int pagelen)
		{
			int recordnum = 0;
			
			System.Collections.IEnumerator dataRows =  dataTable.Rows.GetEnumerator();
			
			while (dataRows.MoveNext() && (pagelen != 0))
			{
				if(recordnum >= start)
				{
					DataRow row = (DataRow)dataRows.Current;
				
					DeliveryNoteDetails c = new DeliveryNoteDetails();
					c.PrescriptionId = (Convert.IsDBNull(row["PrescriptionID"]))?string.Empty:(System.String)row["PrescriptionID"];
					c.DeliveryDate = (Convert.IsDBNull(row["DeliveryDate"]))?DateTime.MinValue:(System.DateTime)row["DeliveryDate"];
					c.PatientCode = (Convert.IsDBNull(row["PatientCode"]))?string.Empty:(System.String)row["PatientCode"];
					c.PatientName = (Convert.IsDBNull(row["PatientName"]))?string.Empty:(System.String)row["PatientName"];
					c.DrugId = (Convert.IsDBNull(row["DrugId"]))?string.Empty:(System.String)row["DrugId"];
					c.DrugName = (Convert.IsDBNull(row["DrugName"]))?string.Empty:(System.String)row["DrugName"];
					c.Unit = (Convert.IsDBNull(row["Unit"]))?string.Empty:(System.String)row["Unit"];
					c.Quantity = (Convert.IsDBNull(row["Quantity"]))?(int)0:(System.Int32)row["Quantity"];
					c.CostPrice = (Convert.IsDBNull(row["CostPrice"]))?0.0m:(System.Decimal)row["CostPrice"];
					c.SellPrice = (Convert.IsDBNull(row["SellPrice"]))?0.0m:(System.Decimal)row["SellPrice"];
					c.ExpDate = (Convert.IsDBNull(row["ExpDate"]))?DateTime.MinValue:(System.DateTime?)row["ExpDate"];
					c.BatchLot = (Convert.IsDBNull(row["BatchLot"]))?string.Empty:(System.String)row["BatchLot"];
					c.AcceptChanges();
					rows.Add(c);
					pagelen -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		*/	
						
		///<summary>
		/// Fill an <see cref="VList&lt;DeliveryNoteDetails&gt;"/> From a DataReader.
		///</summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pageLength">number of row.</param>
		///<returns>a <see cref="VList&lt;DeliveryNoteDetails&gt;"/></returns>
		protected VList<DeliveryNoteDetails> Fill(IDataReader reader, VList<DeliveryNoteDetails> rows, int start, int pageLength)
		{
			int recordnum = 0;
			while (reader.Read() && (pageLength != 0))
			{
				if(recordnum >= start)
				{
					DeliveryNoteDetails entity = null;
					if (DataRepository.Provider.UseEntityFactory)
					{
						entity = EntityManager.CreateViewEntity<DeliveryNoteDetails>("DeliveryNoteDetails",  DataRepository.Provider.EntityCreationalFactoryType); 
					}
					else
					{
						entity = new DeliveryNoteDetails();
					}
					
					entity.SuppressEntityEvents = true;

					entity.PrescriptionId = (System.String)reader[((int)DeliveryNoteDetailsColumn.PrescriptionId)];
					//entity.PrescriptionId = (Convert.IsDBNull(reader["PrescriptionID"]))?string.Empty:(System.String)reader["PrescriptionID"];
					entity.DeliveryDate = (System.DateTime)reader[((int)DeliveryNoteDetailsColumn.DeliveryDate)];
					//entity.DeliveryDate = (Convert.IsDBNull(reader["DeliveryDate"]))?DateTime.MinValue:(System.DateTime)reader["DeliveryDate"];
					entity.PatientCode = (System.String)reader[((int)DeliveryNoteDetailsColumn.PatientCode)];
					//entity.PatientCode = (Convert.IsDBNull(reader["PatientCode"]))?string.Empty:(System.String)reader["PatientCode"];
					entity.PatientName = (System.String)reader[((int)DeliveryNoteDetailsColumn.PatientName)];
					//entity.PatientName = (Convert.IsDBNull(reader["PatientName"]))?string.Empty:(System.String)reader["PatientName"];
					entity.DrugId = (System.String)reader[((int)DeliveryNoteDetailsColumn.DrugId)];
					//entity.DrugId = (Convert.IsDBNull(reader["DrugId"]))?string.Empty:(System.String)reader["DrugId"];
					entity.DrugName = (System.String)reader[((int)DeliveryNoteDetailsColumn.DrugName)];
					//entity.DrugName = (Convert.IsDBNull(reader["DrugName"]))?string.Empty:(System.String)reader["DrugName"];
					entity.Unit = (System.String)reader[((int)DeliveryNoteDetailsColumn.Unit)];
					//entity.Unit = (Convert.IsDBNull(reader["Unit"]))?string.Empty:(System.String)reader["Unit"];
					entity.Quantity = (System.Int32)reader[((int)DeliveryNoteDetailsColumn.Quantity)];
					//entity.Quantity = (Convert.IsDBNull(reader["Quantity"]))?(int)0:(System.Int32)reader["Quantity"];
					entity.CostPrice = (System.Decimal)reader[((int)DeliveryNoteDetailsColumn.CostPrice)];
					//entity.CostPrice = (Convert.IsDBNull(reader["CostPrice"]))?0.0m:(System.Decimal)reader["CostPrice"];
					entity.SellPrice = (System.Decimal)reader[((int)DeliveryNoteDetailsColumn.SellPrice)];
					//entity.SellPrice = (Convert.IsDBNull(reader["SellPrice"]))?0.0m:(System.Decimal)reader["SellPrice"];
					entity.ExpDate = (reader.IsDBNull(((int)DeliveryNoteDetailsColumn.ExpDate)))?null:(System.DateTime?)reader[((int)DeliveryNoteDetailsColumn.ExpDate)];
					//entity.ExpDate = (Convert.IsDBNull(reader["ExpDate"]))?DateTime.MinValue:(System.DateTime?)reader["ExpDate"];
					entity.BatchLot = (reader.IsDBNull(((int)DeliveryNoteDetailsColumn.BatchLot)))?null:(System.String)reader[((int)DeliveryNoteDetailsColumn.BatchLot)];
					//entity.BatchLot = (Convert.IsDBNull(reader["BatchLot"]))?string.Empty:(System.String)reader["BatchLot"];
					entity.AcceptChanges();
					entity.SuppressEntityEvents = false;
					
					rows.Add(entity);
					pageLength -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		
		
		/// <summary>
		/// Refreshes the <see cref="DeliveryNoteDetails"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="DeliveryNoteDetails"/> object to refresh.</param>
		protected void RefreshEntity(IDataReader reader, DeliveryNoteDetails entity)
		{
			reader.Read();
			entity.PrescriptionId = (System.String)reader[((int)DeliveryNoteDetailsColumn.PrescriptionId)];
			//entity.PrescriptionId = (Convert.IsDBNull(reader["PrescriptionID"]))?string.Empty:(System.String)reader["PrescriptionID"];
			entity.DeliveryDate = (System.DateTime)reader[((int)DeliveryNoteDetailsColumn.DeliveryDate)];
			//entity.DeliveryDate = (Convert.IsDBNull(reader["DeliveryDate"]))?DateTime.MinValue:(System.DateTime)reader["DeliveryDate"];
			entity.PatientCode = (System.String)reader[((int)DeliveryNoteDetailsColumn.PatientCode)];
			//entity.PatientCode = (Convert.IsDBNull(reader["PatientCode"]))?string.Empty:(System.String)reader["PatientCode"];
			entity.PatientName = (System.String)reader[((int)DeliveryNoteDetailsColumn.PatientName)];
			//entity.PatientName = (Convert.IsDBNull(reader["PatientName"]))?string.Empty:(System.String)reader["PatientName"];
			entity.DrugId = (System.String)reader[((int)DeliveryNoteDetailsColumn.DrugId)];
			//entity.DrugId = (Convert.IsDBNull(reader["DrugId"]))?string.Empty:(System.String)reader["DrugId"];
			entity.DrugName = (System.String)reader[((int)DeliveryNoteDetailsColumn.DrugName)];
			//entity.DrugName = (Convert.IsDBNull(reader["DrugName"]))?string.Empty:(System.String)reader["DrugName"];
			entity.Unit = (System.String)reader[((int)DeliveryNoteDetailsColumn.Unit)];
			//entity.Unit = (Convert.IsDBNull(reader["Unit"]))?string.Empty:(System.String)reader["Unit"];
			entity.Quantity = (System.Int32)reader[((int)DeliveryNoteDetailsColumn.Quantity)];
			//entity.Quantity = (Convert.IsDBNull(reader["Quantity"]))?(int)0:(System.Int32)reader["Quantity"];
			entity.CostPrice = (System.Decimal)reader[((int)DeliveryNoteDetailsColumn.CostPrice)];
			//entity.CostPrice = (Convert.IsDBNull(reader["CostPrice"]))?0.0m:(System.Decimal)reader["CostPrice"];
			entity.SellPrice = (System.Decimal)reader[((int)DeliveryNoteDetailsColumn.SellPrice)];
			//entity.SellPrice = (Convert.IsDBNull(reader["SellPrice"]))?0.0m:(System.Decimal)reader["SellPrice"];
			entity.ExpDate = (reader.IsDBNull(((int)DeliveryNoteDetailsColumn.ExpDate)))?null:(System.DateTime?)reader[((int)DeliveryNoteDetailsColumn.ExpDate)];
			//entity.ExpDate = (Convert.IsDBNull(reader["ExpDate"]))?DateTime.MinValue:(System.DateTime?)reader["ExpDate"];
			entity.BatchLot = (reader.IsDBNull(((int)DeliveryNoteDetailsColumn.BatchLot)))?null:(System.String)reader[((int)DeliveryNoteDetailsColumn.BatchLot)];
			//entity.BatchLot = (Convert.IsDBNull(reader["BatchLot"]))?string.Empty:(System.String)reader["BatchLot"];
			reader.Close();
	
			entity.AcceptChanges();
		}
		
		/*
		/// <summary>
		/// Refreshes the <see cref="DeliveryNoteDetails"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="DeliveryNoteDetails"/> object.</param>
		protected static void RefreshEntity(DataSet dataSet, DeliveryNoteDetails entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.PrescriptionId = (Convert.IsDBNull(dataRow["PrescriptionID"]))?string.Empty:(System.String)dataRow["PrescriptionID"];
			entity.DeliveryDate = (Convert.IsDBNull(dataRow["DeliveryDate"]))?DateTime.MinValue:(System.DateTime)dataRow["DeliveryDate"];
			entity.PatientCode = (Convert.IsDBNull(dataRow["PatientCode"]))?string.Empty:(System.String)dataRow["PatientCode"];
			entity.PatientName = (Convert.IsDBNull(dataRow["PatientName"]))?string.Empty:(System.String)dataRow["PatientName"];
			entity.DrugId = (Convert.IsDBNull(dataRow["DrugId"]))?string.Empty:(System.String)dataRow["DrugId"];
			entity.DrugName = (Convert.IsDBNull(dataRow["DrugName"]))?string.Empty:(System.String)dataRow["DrugName"];
			entity.Unit = (Convert.IsDBNull(dataRow["Unit"]))?string.Empty:(System.String)dataRow["Unit"];
			entity.Quantity = (Convert.IsDBNull(dataRow["Quantity"]))?(int)0:(System.Int32)dataRow["Quantity"];
			entity.CostPrice = (Convert.IsDBNull(dataRow["CostPrice"]))?0.0m:(System.Decimal)dataRow["CostPrice"];
			entity.SellPrice = (Convert.IsDBNull(dataRow["SellPrice"]))?0.0m:(System.Decimal)dataRow["SellPrice"];
			entity.ExpDate = (Convert.IsDBNull(dataRow["ExpDate"]))?DateTime.MinValue:(System.DateTime?)dataRow["ExpDate"];
			entity.BatchLot = (Convert.IsDBNull(dataRow["BatchLot"]))?string.Empty:(System.String)dataRow["BatchLot"];
			entity.AcceptChanges();
		}
		*/
			
		#endregion Helper Functions
	}//end class

	#region DeliveryNoteDetailsFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="DeliveryNoteDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class DeliveryNoteDetailsFilterBuilder : SqlFilterBuilder<DeliveryNoteDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsFilterBuilder class.
		/// </summary>
		public DeliveryNoteDetailsFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public DeliveryNoteDetailsFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public DeliveryNoteDetailsFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion DeliveryNoteDetailsFilterBuilder

	#region DeliveryNoteDetailsParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="DeliveryNoteDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class DeliveryNoteDetailsParameterBuilder : ParameterizedSqlFilterBuilder<DeliveryNoteDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsParameterBuilder class.
		/// </summary>
		public DeliveryNoteDetailsParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public DeliveryNoteDetailsParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public DeliveryNoteDetailsParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion DeliveryNoteDetailsParameterBuilder
	
	#region DeliveryNoteDetailsSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="DeliveryNoteDetails"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class DeliveryNoteDetailsSortBuilder : SqlSortBuilder<DeliveryNoteDetailsColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the DeliveryNoteDetailsSqlSortBuilder class.
		/// </summary>
		public DeliveryNoteDetailsSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion DeliveryNoteDetailsSortBuilder

} // end namespace
